package cn.wolfcode.cotroller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.domain.pageBean;
import cn.wolfcode.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpCotroller {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String  name,
                       Integer  gender,
                       @DateTimeFormat(pattern ="yyyy-MM-dd") Date begin,
                        @DateTimeFormat(pattern ="yyyy-MM-dd") Date end){
     log.info("分页查询员工信息",page,pageSize);
      pageBean pageBean =empService.page(page,pageSize,name,gender,begin,end);
      return Result.success(pageBean);
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids){
        log.info("删除{}员工信息",ids);
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("保存员工信息");
        empService.add(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selById(@PathVariable Long id){
        log.info("查询员工信息");
        Emp emp = empService.selById(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工信息");
        empService.update(emp);
        return Result.success();
    }
}
